import Taro from '@tarojs/taro'
import storage from './storage'
let token = storage.get('token') || '00000000000'
// if(process.env.NODE_ENV === 'development'){
//   开发环境
// } else if (process.env.NODE_ENV === 'production'){
//   生产环境
// }
function request (params, method = 'GET') {
  return new Promise((resolve,reject)=>{
    // 开启全局加载
    Taro.showLoading({title:'加载中'})
    let { url, data } = params
    let contentType = 'application/x-www-form-urlencoded'
    contentType = params.contentType || contentType
    return Taro.request({
      isShowLoading: false,
      url: BASE_API_URL + url,
      data: data,
      method: method,
      header: { 'content-type': contentType, 'token': token }, // 默认contentType ,预留token
      // eslint-disable-next-line no-unused-vars
      success(res) {
        Taro.hideLoading()
        resolve(res)
      },  
      error(e) {
        Taro.hideLoading()
        // eslint-disable-next-line no-undef
        reject(logError('api', '请求接口出现问题'))
      }
    })
  })
}
export const get = (url, data = '') =>{
  let option = { url, data }
  return request(option)
}

export const post = (url, data, contentType)=> {
  let params = { url, data, contentType }
  return request(params, 'POST')
}


